package com.common.account.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.common.account.entity.AuPermission;
import com.common.basis.oauth.AuUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Mapper 接口
 *
 * @author wangguochao
 * @since 2020-01-06
 */
public interface AuPermissionMapper extends BaseMapper<AuPermission> {

    /**
     * user权限
     *
     * @param userName 用户账号
     * @return 权限
     */
    @Select("SELECT _ap.id," +
            "       _ap.create_by," +
            "       _ap.created," +
            "       _ap.last_modified_by," +
            "       _ap.permission_code," +
            "       _ap.permission_groups," +
            "       _ap.permission_name," +
            "       _ap.permission_url," +
            "       _ap.updated," +
            "       _ap.is_del," +
            "       _ap.parent_id," +
            "       _ap.icon," +
            "       _ap.frontend_link," +
            "       _ap.front_end_template," +
            "       _ap.exe," +
            "       _ap.style," +
            "       _ap.sort_number," +
            "       _ap.sort_time," +
            "       _ap.page_visible," +
            "       _ap.line_display," +
            "       _ap.tenant_id" +
            " FROM (SELECT DISTINCT _rap.permission" +
            "      FROM au_user _au" +
            "               JOIN user_and_role _uar ON _au.id = _uar.user_id" +
            "               JOIN au_role _ar ON _ar.id = _uar.role_id" +
            "               JOIN permission_and_role_directly_under _rap on _rap.role_id = _ar.id" +
            "      where _au.user_name = #{userName}) t" +
            "         JOIN au_permission _ap ON t.permission = _ap.id" +
            " WHERE _ap.is_del = 0" +
            " order by _ap.sort_number desc, _ap.sort_time desc, _ap.created desc;")
    List<AuPermission> findTreeByUserName(@Param("userName") String userName);

    /**
     * 根据用户查询权限
     *
     * @param userId 用户ID
     * @return 权限
     */
    @Select("select DISTINCT ap.permission_code " +
            "from au_user au " +
            "         join user_and_role uar on uar.user_id = au.id " +
            "         join permission_and_role par on par.role_id = uar.role_id " +
            "         join au_permission ap on ap.id = par.permission " +
            "where au.id = #{userId}" +
            " and ap.is_del = 0 " +
            " and au.is_del = 0 " +
            " and au.enabled = 1")
    List<String> findByUserId(String userId);

    /**
     * 根据权限查询人员
     *
     * @param permission 权限
     * @return 人员
     */
    @Select(" select au.* " +
            " from au_user au " +
            "         join user_and_role uar on uar.user_id = au.id " +
            "         join permission_and_role par on par.role_id = uar.role_id " +
            "         join au_permission ap on ap.id = par.permission " +
            " where ap.permission_code =  #{permission} " +
            " and au.enabled = 1 " +
            " and ap.is_del = 0" +
            " and au.is_account = 0")
    List<AuUser> findUserByPermission(String permission);

    /**
     * 获取用户权限按照类型
     *
     * @param name 用户ID
     * @param type 类型
     * @return code
     */
    @Select("select DISTINCT ap.permission_code " +
            "from au_user au " +
            "         join user_and_role uar on uar.user_id = au.id " +
            "         join permission_and_role par on par.role_id = uar.role_id " +
            "         join au_permission ap on ap.id = par.permission " +
            "where au.user_name = #{name} and ap.permission_groups = #{type}  and ap.is_del = 0")
    List<String> findByUserNameAndType(String name, String type);

    /**
     * 获取最大排序数量根据父id
     *
     * @param parentId 父id
     * @return {@code Integer}
     */
    @Select("select max(ap.sort_number) from au_permission ap where parent_id = #{parentId}")
    Integer getMaxSortNumberByParentId(@Param("parentId") String parentId);
}
